//
Function.prototype.myCall = function (context, ...args) {
  context = context || window;
  context.fn = this;
  var arr = args.map((item, i) => `args[${i}]`);
  var result = eval(`context.fn(${arr})`);
  delete context.fn;
  return result;
};

function Person(name) {
  this.name = name;
  console.log(this);
}
Person.myCall({ a: 212 }, 111);

Function.prototype.myApply = function (context, args) {
  context = context || window;
  context.fn = this;
  var result = eval(`context.fn(...args)`);
  delete context.fn;
  return result;
};

Function.prototype.myBind = function (context, ...args) {
  if (typeof this !== 'function') {
    return new Error();
  }
  var self = this;
  var fn = function () {
    // 处理bind绑定的时候，this指向的问题
    self.apply(
      this instanceof fn ? this : context,
      args.concat(Array.prototype.slice.call(arguments)),
    );
  };
  if (this.prototype) {
    fn.prototype = Object.create(this.prototype);
    fn.prototype.constructor = fn;
  }
  return fn;
};


function newObj(func, ...args) {
  if (typeof func !== 'function') {
    return new Error('请传入一个构造函数创建对象');
  }
  var obj = {};
  // 生成原型链
  obj.__proto__ = Object.create(func);
  // 属性获取
  var result = func.apply(obj, [...args]);
  var isObj = result !== null && typeof result === 'object';
  return isObj ? result : obj;
}

// 数组变成一个树结构
// [{id: 11, parentId: null}, {id:22, parentId: 11}, {id: 33, parentId: 22}]
/* 
  {
    id: 11,
    children: [
      {
        id: 22,
        name: '22',
        children: [
          {
            id: 22,
            name: '33'
            children: null,
          }
        ]
      }
    ]
  }
*/
const changeData = (arr) => {

  const _change = (arr, root) => {
    arr.forEach((item) => {
      if (item.parnetId === undefined) {
      }
    })
  }
  const result = _change(arr, {});
  return arr;
}